package com.zyw.module.console.qywx.api;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.zyw.module.console.qywx.api.response.AccessTokenResponse;
import com.zyw.module.console.qywx.api.response.WxUserDeatilResponse;
import com.zyw.module.console.qywx.api.response.WxUserResponse;
import com.zyw.net.http.HttpParams;
import com.zyw.net.supports.BaseApi;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangyw
 * @date 2017/3/31 9:47
 */
public class AccessTokenApi extends BaseApi {


    private static String code;

    public static String getCode() {
        return code;
    }

    public static void setCode(String code) {
        AccessTokenApi.code = code;
    }

    private static final String API_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";

    private static final String USER_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";

    private static final String USER_DETAIL_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get";


    private static Cache<String, String> cache = CacheBuilder.newBuilder()
            .maximumSize(100).expireAfterWrite(2, TimeUnit.HOURS)
            .recordStats()
            .build();

    public static String get(final String key) throws Exception {

        String var = cache.get(key, new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("如果没有值,就执行其他方式去获取值");
                if (key.equals("accessToken")) {
                    return getAccessToken();
                } else if (key.equals("userid")) {
                    return getUserInfo();
                }
                return "";
            }
        });
        return var;
    }


    public static String getAccessToken() {
        HttpParams httpParams = HttpParams.create();
        httpParams.addParameter("corpid", "wxe4dcc39438de23c5");
        httpParams.addParameter("corpsecret", "zGj1nNpS4OMBPEO6V6dviHVpdGYjyn15Y93k84dTPavnaZ7AJMEAPJmbpeTT-g3c");
        AccessTokenResponse response = get(API_URL, httpParams, AccessTokenResponse.class);//get(API_URL, httpParams, AccessTokenResponse.class);
        return response.getAccess_token();
    }

    public static String getUserInfo() throws Exception {
        HttpParams httpParams = HttpParams.create();
        httpParams.addParameter("access_token", AccessTokenApi.get("accessToken"));
        httpParams.addParameter("code", code);
        WxUserResponse response = get(USER_URL, httpParams, WxUserResponse.class);
        return response.getUserId();
    }

    public static WxUserDeatilResponse getUserInfoDetail(String code) throws Exception {
        setCode(code);

        HttpParams httpParams = HttpParams.create();
        httpParams.addParameter("access_token", AccessTokenApi.get("accessToken"));
        httpParams.addParameter("userid", AccessTokenApi.get("userid"));
        WxUserDeatilResponse response = get(USER_DETAIL_URL, httpParams, WxUserDeatilResponse.class);


        return response;
    }


}
